package com.nj.dms.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.Essay;
import com.nj.dms.dao.EssayDao;
import com.nj.dms.dao.impl.EssayDaoImpl;

/**
 * 保存修改的文章
 */
@WebServlet("/SaveEssay")
public class SaveEssay extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int id = new Integer(request.getParameter("id"));
        String title = request.getParameter("title");
        String version = request.getParameter("version");
        int thick = new Integer(request.getParameter("thick"));
        String digest = request.getParameter("digest");
        String content = request.getParameter("content");

        int audit = thick == 1 ? 1 : 0;

        Essay essay = new Essay(id, title, digest, content, version, thick, audit, new Date());

        EssayDao essayDao = new EssayDaoImpl();

        if (essayDao.saveEssay(essay)) {

            // 保存文章成功，跳转到文章列表

            response.sendRedirect("MyEssayList");

        } else {

            // 保存文章失败，服务器跳转到修改编辑页面

            response.sendRedirect("UpdateEssay?id=" + id);

        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
